package cn.iocoder.yudao.module.ai.controller.admin.picture;

import cn.iocoder.yudao.module.ai.common.IdUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.picture.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.picture.PictureDO;
import cn.iocoder.yudao.module.ai.service.picture.PictureService;

@Tag(name = "管理后台 - 图片")
@RestController
@RequestMapping("/ai/picture")
@Validated
public class PictureController {

    @Resource
    private PictureService pictureService;

    @PostMapping("/create")
    @Operation(summary = "创建图片")
    @PreAuthorize("@ss.hasPermission('ai:picture:create')")
    public CommonResult<String> createPicture(@Valid @RequestBody PictureReqVO createReqVO) {
        createReqVO.setId(IdUtils.generateId());
        return success(pictureService.createPicture(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新图片")
    @PreAuthorize("@ss.hasPermission('ai:picture:update')")
    public CommonResult<Boolean> updatePicture(@Valid @RequestBody PictureReqVO updateReqVO) {
        pictureService.updatePicture(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除图片")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:picture:delete')")
    public CommonResult<Boolean> deletePicture(@RequestParam("id") String id) {
        pictureService.deletePicture(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得图片")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:picture:query')")
    public CommonResult<PictureRespVO> getPicture(@RequestParam("id") String id) {
        PictureDO picture = pictureService.getPicture(id);
        return success(BeanUtils.toBean(picture, PictureRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得图片分页")
    @PreAuthorize("@ss.hasPermission('ai:picture:query')")
    public CommonResult<PageResult<PictureRespVO>> getPicturePage(@Valid PicturePageReqVO pageReqVO) {
        PageResult<PictureDO> pageResult = pictureService.getPicturePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PictureRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出图片 Excel")
    @PreAuthorize("@ss.hasPermission('ai:picture:export')")
    @OperateLog(type = EXPORT)
    public void exportPictureExcel(@Valid PicturePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PictureDO> list = pictureService.getPicturePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "图片.xls", "数据", PictureRespVO.class,
                        BeanUtils.toBean(list, PictureRespVO.class));
    }

}